# GoX 项目 Makefile

.PHONY: clean build test generate gox-compile all help

# 默认目标
all: gox-compile build

# 帮助信息
help:
	@echo "GoX 项目构建命令："
	@echo "  make clean        - 清理生成的文件"
	@echo "  make gox-compile  - 编译 GoX 源文件"
	@echo "  make build        - 构建 Go 项目"
	@echo "  make test         - 运行测试"
	@echo "  make generate     - 运行 go generate"
	@echo "  make all          - 执行完整构建流程"

# 清理生成的文件
clean:
	@echo "🧹 清理生成的文件..."
	@find . -name "*.go" ! -name "*.gox.go" ! -name "extension.js" -delete 2>/dev/null || true
	@echo "✅ 清理完成"

# 编译 GoX 源文件
gox-compile:
	@echo "🔨 编译 GoX 源文件..."
	@for gox_file in *.gox; do \
		if [ -f "$$gox_file" ]; then \
			output_file=$${gox_file%.gox}.go; \
			echo "  处理: $$gox_file -> $$output_file"; \
			if command -v goxc >/dev/null 2>&1; then \
				goxc -i "$$gox_file" -o "$$output_file"; \
			elif [ -f "../gox-lsp.exe" ]; then \
				../gox-lsp.exe -compile -i "$$gox_file" -o "$$output_file"; \
			else \
				echo "⚠️  GoX 编译器未找到，使用模拟编译..."; \
				sed '/^\/\/go:build ignore/d; /^\/\/ +build ignore/d; /^\/\/go:generate/d' "$$gox_file" > "$$output_file"; \
			fi; \
			echo "  ✅ 生成: $$output_file"; \
		fi; \
	done

# 运行 go generate
generate: gox-compile
	@echo "🔄 运行 go generate..."
	go generate ./...

# 构建 Go 项目
build: gox-compile
	@echo "🔨 构建 Go 项目..."
	go build -v ./...
	@echo "✅ 构建完成"

# 运行测试
test: gox-compile
	@echo "🧪 运行测试..."
	go test ./...

# 开发模式：监听文件变化并自动编译
dev:
	@echo "🔄 开发模式：监听文件变化..."
	@if command -v fswatch >/dev/null 2>&1; then \
		fswatch -o *.gox.go | xargs -n1 -I{} make gox-compile; \
	elif command -v inotifywait >/dev/null 2>&1; then \
		while inotifywait -e modify *.gox.go; do make gox-compile; done; \
	else \
		echo "⚠️  需要安装 fswatch 或 inotifywait 来启用监听模式"; \
	fi

# 安装依赖
deps:
	@echo "📦 安装依赖..."
	go mod tidy
	go mod download

# 格式化代码
fmt: gox-compile
	@echo "🎨 格式化代码..."
	go fmt ./...
	gofmt -w *.go 2>/dev/null || true

# 代码检查
lint: gox-compile
	@echo "🔍 代码检查..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		go vet ./...; \
	fi 